<?
/**
 * @id $Id: goodsCateManager.php 4 2007-05-25 12:59:37Z gamezoom $
 * @author $Author: gamezoom $
 * @version $Revision: 4 $
 * @date $Date: 2007-05-25 12:59:37 +0000 (Fri, 25 May 2007) $
 * @svn $HeadURL: http://jxcfortaobao.googlecode.com/svn/trunk/core/www/goodsCateManager.php $
 */

L2P::using('inc.abstractWwwModule');

class goodsCateManager extends abstractWwwModule
{
	function defaultEvent()
	{
		return $this->showManagerForm();
	}

	function showManagerForm()
	{
		self::needLogined();

		return self::display("goodsCateManagerForm");
	}

	function showEditForm()
	{
		self::needLogined();

		if(isset($_GET["id"]) && $_GET["id"] > 0){
			$goods_cate = pdoFactory::fetch("SELECT id, fid, name FROM goods_cate WHERE id=" . ($_GET["id"] + 0));
			self::assign("goods_cate", $goods_cate);
		}else{
			self::assign("goods_cate", $_POST);
		}

		return self::display("goodsCateEditForm");
	}

	function save()
	{
		self::needLogined();

		$id = isset($_POST["id"]) ? $_POST["id"] + 0 : 0;
		$fid = isset($_POST["fid"]) ? $_POST["fid"] + 0 : 0;
		$name = isset($_POST["name"]) ? trim($_POST["name"]) : "";

		$errors = array();
		$len = strlen($name);
		if($len < 1 || $len > 200){
			$errors["name"] = "name length error";
		}else{
			$d = pdoFactory::fetch("SELECT * FROM goods_cate WHERE name=:name", array(":name" => $name));
			if($d && $d["id"] <> $id){
				$errors["name"] = "name is exists";
			}
		}

		if($fid > 0){
			$father = pdoFactory::fetch("SELECT path FROM goods_cate WHERE id=" . $fid);
			if($father){
				$path = $father["path"];
			}else{
				$errors["fid"] = "fid is unknown";
			}
		}else{
			$fid = 0;
			$father = array("path" => ",");
			$path = ",";
		}
		
		if(count($errors) == 0){
			//create goods category
			if($id == 0){
				pdoFactory::exec("INSERT INTO goods_cate(fid, name) VALUES(:fid, :name)", array(":fid" => $fid, ":name" => $name));
				$d = pdoFactory::fetch("SELECT LAST_INSERT_ID() AS id FROM goods_cate");
				$last_insert_id = $d["id"];
				pdoFactory::exec("UPDATE goods_cate SET ord=:ord, path=:path WHERE id=:id", array(":ord" => $last_insert_id, ":path" => $path . $last_insert_id . ",", ":id" => $last_insert_id));

				$this->logEvent("goodsCateManager", "create goods category:\t{$last_insert_id}");

			//modify goods category
			}else{
				$gc = pdoFactory::fetch("SELECT * FROM goods_cate WHERE id=" . $id);

				pdoFactory::exec("UPDATE goods_cate SET name=:name, fid=:fid WHERE id=:id", array(":name" => $name, ":fid" => $fid, ":id" => $id));

				//changed fid
				if($gc["fid"] != $fid){
					$oldPath = $gc["path"];
					$nPath = $path . $id . ",";
					pdoFactory::exec("UPDATE goods_cate SET path = REPLACE(path, '{$oldPath}', '{$nPath}') WHERE INSTR(path, '{$oldPath}') > 0");
					//echo "UPDATE goods_cate SET path=REPLACE(path, '{$oldPath}', '{$nPath}') WHERE INSTR(path, '{$oldPath}') > 0";exit();


					//new fathers + goods count
					$ids = substr($father["path"], 1, strlen($father["path"]) - 2);
					pdoFactory::exec("UPDATE goods_cate SET goods_count = goods_count + " . $gc["goods_count"] . " WHERE id IN ({$ids})");
					//echo "UPDATE goods_cate SET goods_count = goods_count + " . $gc["goods_count"] . " WHERE id IN ({$ids})" . "<br />";

					//old father - goods count
					$ids = explode(",", $gc["path"]);
					$ids = array_slice($ids, 1, count($ids) - 3);
					$ids = implode(",", $ids);
					pdoFactory::exec("UPDATE goods_cate SET goods_count = goods_count - " . $gc["goods_count"] . " WHERE id IN ({$ids})");
					//echo "UPDATE goods_cate SET goods_count = goods_count - " . $gc["goods_count"] . " WHERE id IN ({$ids})" . "<br />";
				}

				$this->logEvent("goodsCateManager", "modify goods category:\t{$id}");
			}
			
			header("Location: ?module=goodsCateManager");
		}

		self::assign("errors", $errors);
		return $this->showEditForm();
	}

	function moveUp()
	{
		self::needLogined();

		$id = isset($_GET["id"]) ? $_GET["id"] + 0 : 0;
		$gc = pdoFactory::fetch("SELECT * FROM goods_cate WHERE id={$id}");
		$f_gc = pdoFactory::fetch("SELECT * FROM goods_cate WHERE ord < $gc[ord] AND fid=$gc[fid] ORDER BY ord DESC LIMIT 1");
		if($f_gc){
			pdoFactory::exec("UPDATE goods_cate SET ord = {$gc[ord]} WHERE id = $f_gc[id]");
			pdoFactory::exec("UPDATE goods_cate SET ord = {$f_gc[ord]} WHERE id = $gc[id]");
		}

		header("Location: ?module=goodsCateManager");
	}

	function moveDown()
	{
		self::needLogined();

		$id = isset($_GET["id"]) ? $_GET["id"] + 0 : 0;
		$gc = pdoFactory::fetch("SELECT * FROM goods_cate WHERE id={$id}");
		$n_gc = pdoFactory::fetch("SELECT * FROM goods_cate WHERE ord > $gc[ord] AND fid=$gc[fid] ORDER BY ord ASC LIMIT 1");
		if($n_gc){
			pdoFactory::exec("UPDATE goods_cate SET ord = {$gc[ord]} WHERE id = $n_gc[id]");
			pdoFactory::exec("UPDATE goods_cate SET ord = {$n_gc[ord]} WHERE id = $gc[id]");
		}

		header("Location: ?module=goodsCateManager");
	}
}
?>